home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.uv.es
/
2014.11.ftp.uv.es.tar
/
ftp.uv.es
/
pub
/
mirror
/
MailDrop
/
plug-ins
/
SamplePlugInProjects.hqx
/
Sample Plug-in Projects
/
Old Address Books
/
Old Address Books.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-01-08
|
6KB
|
331 lines
#include "Mail Drop PlugIns.h"
#ifndef __powerc
#include <SetupA4.h>
#include <A4Stuff.h>
#else
ProcInfoType __procinfo = uppMailDropPlugInProcInfo;
#endif
OSErr ConvertFile(MailDropImportParamPtr pb);
void ParseRecipientString(char *theString, char *address, char *realname, short *how);
Boolean ValidAddressChar(char ch);
Boolean ValidRealNameChar(char ch);
long clen(char *cptr);
char *ccpy(char *s1, char *s2);
char *ccat(char *s1, char *s2);
short ccmp(char *s1, char *s2);
short cncmp(char *s1, char *s2, short n);
void c2p(char *cptr);
void p2c(StringPtr cptr);
pascal OSErr main(short selector, MailDropPlugInPBPtr plugInPBPtr, void *plugInData)
{
MailDropImportParam *pb;
OSErr err;
short refNum;
short vRefNum;
long dirID;
FSSpec spec;
Handle strHandle;
#ifndef __powerc
long oldA4;
#endif
#ifndef __powerc
oldA4 = SetCurrentA4();
RememberA4();
#endif
pb = (MailDropImportParam *)plugInPBPtr;
if (pb == nil) return errNullParamBlock;
if (pb->type != plugInAddressImport) return errWrongPlugInType;
err = noErr;
switch (selector) {
case selectorImportSetTypeCreator:
pb->fileType = 'Addr';
pb->fileCreator = 'MDrp';
break;
case selectorImportFile:
err = ConvertFile(pb);
break;
case selectorFinish:
if (pb->addresses != nil) DisposeHandle(pb->addresses);
break;
default:
err = errInvalidSelector;
break;
}
#ifndef __powerc
SetA4(oldA4);
#endif
return err;
}
OSErr ConvertFile(MailDropImportParamPtr pb)
{
OSErr err;
short refNum;
long numChars;
Handle data;
Boolean done = false;
char *aline, *nextline, address[256], realname[256];
short how;
char *tempStr, theWholeThing[512];
pb->addresses = nil; // just in case
err = FSpOpenDF(&pb->importFile, fsRdPerm, &refNum);
if (err != noErr) return err;
err = GetEOF(refNum, &numChars);
if (err != noErr) {
FSClose(refNum);
return err;
}
data = NewHandleClear(numChars);
if (data == nil) {
data = TempNewHandle(numChars, &err);
if ((data == nil) || (err != noErr)) {
FSClose(refNum);
return (err == noErr) ? memFullErr : err;
}
}
HLock(data);
err = FSRead(refNum, &numChars, *data);
FSClose(refNum);
pb->addresses = NewHandleClear(0);
if (pb->addresses == nil) {
DisposeHandle(data);
return memFullErr;
}
done = false;
aline = nextline = *data;
do {
while ((*nextline != 0) && (*nextline != 13)) nextline++;
if (*nextline == 0) done = true; // current string is last string
if ((*nextline == 13) && (*(nextline+1) == 0)) done = true;
else *nextline++ = 0;
ParseRecipientString(aline, address, realname, &how);
if (address[0] != 0) {
if (realname[0] == 0) {
ccpy(theWholeThing, address);
ccat(theWholeThing, "\r");
}
else {
ccpy(theWholeThing, "\"");
ccat(theWholeThing, realname);
ccat(theWholeThing, "\" <");
ccat(theWholeThing, address);
ccat(theWholeThing, ">\r");
}
err = PtrAndHand(theWholeThing, pb->addresses, clen(theWholeThing));
if (err != noErr) {
DisposeHandle(pb->addresses);
pb->addresses = nil;
HUnlock(data);
DisposeHandle(data);
return err;
}
}
aline = nextline;
} while (!done);
err = PtrAndHand("", pb->addresses, 1);
if (err != noErr) {
DisposeHandle(pb->addresses);
pb->addresses = nil;
HUnlock(data);
DisposeHandle(data);
return err;
}
HUnlock(data);
DisposeHandle(data);
return noErr;
}
void ParseRecipientString(char *theString, char *address, char *realname, short *how)
{
Boolean doingaddress;
Boolean doingrealname;
Boolean doinghow;
short i, j;
address[0] = 0;
realname[0] = 0;
*how = 0;
doingaddress = true;
doingrealname = false;
doinghow = false;
i = j = 0;
while (i < clen(theString)) {
switch (theString[i]) {
case 0: // null
case 13: // cr
return;
break;
case 9: // tab
i++;
if (doingaddress) {
doingaddress = false;
doingrealname = true;
}
else if (doingrealname) {
doingrealname = false;
doinghow = true;
}
j = 0;
break;
default:
if (doingaddress) {
if (ValidAddressChar(theString[i])) {
address[j++] = theString[i];
address[j] = 0;
}
}
else if (doingrealname) {
if (ValidRealNameChar(theString[i])) {
if (theString[i] == '\"') realname[j++] = '\'';
else realname[j++] = theString[i];
realname[j] = 0;
}
}
else {
switch (theString[i]) {
case 'T':
case 't':
*how = 0;
break;
case 'C':
case 'c':
*how = 0;
break;
case 'B':
case 'b':
*how = 0;
break;
default:
*how = 0;
break;
}
}
i++;
break;
}
}
}
Boolean ValidAddressChar(char ch)
{
if (ch < 32) return false;
if (ch == '<') return false;
if (ch == '>') return false;
if (ch == '\"') return false;
if (ch == '\\') return false;
if (ch == '\'') return false;
if (ch == ',') return false;
if (ch >= 127) return false;
return true;
}
Boolean ValidRealNameChar(char ch)
{
if (ch < 32) return false;
if (ch == '<') return false;
if (ch == '>') return false;
if (ch == '\\') return false;
if (ch == '(') return false;
if (ch == ')') return false;
if (ch == '@') return false;
if (ch >= 127) return false;
return true;
}
// String Stuff (so you don't need ANSI libs)
long clen(char *cptr)
{
long i;
for (i = 0; cptr[i]; ++i);
return(i);
}
char *ccpy(char *s1, char *s2)
{
char *c1, *c2;
c1 = s1;
c2 = s2;
while (*c1++ = *c2++);
return(s1);
}
char *ccat(char *s1, char *s2)
{
ccpy(s1 + clen(s1), s2);
return(s1);
}
short ccmp(char *s1, char *s2)
{
char *c1, *c2;
c1 = s1;
c2 = s2;
for (; *c1 == *c2; c1++, c2++)
if (*c1 == '\0') return 0;
return *c1 - *c2;
}
short cncmp(char *s1, char *s2, short n)
{
char *c1, *c2;
c1 = s1;
c2 = s2;
if (n == 0) return 0;
for (; ((*c1 == *c2) && (n != 1)); c1++, c2++, n--)
if (*c1 == '\0') return 0;
return *c1 - *c2;
}
void c2p(char *cptr)
{
char len;
BlockMove(cptr, cptr + 1, len = clen(cptr));
*cptr = len;
}
void p2c(StringPtr cptr)
{
char len;
BlockMove(cptr + 1, cptr, len = *cptr);
cptr[len] = 0;
}